package Core::Meta;

use strict;
use warnings;
use vars qw{ @ISA @EXPORT };
use Exporter;


@ISA = qw( Exporter );
@EXPORT = qw (  
	Meta
);
 
sub Meta
#*****************************************************************************
# PURPOSE:
# USAGE: 
# PASSED ARGS:
# RETURN ARGS:
# $sError - Fail Error Message
#*****************************************************************************
{
    my $rhMeta = Core::Meta->new( @_ );
    return $rhMeta;
}
 
 
sub new
#*****************************************************************************
# PURPOSE:
# USAGE: 
# PASSED ARGS:
# RETURN ARGS:
# $sError - Fail Error Message
#*****************************************************************************
{
	my( $rhClass ) = shift;               # Object    
    my( $rhSelf ) = {};
    bless( $rhSelf, $rhClass);
    
	my( $sName )    =   shift; 
    if( $sName ){
		$rhSelf->setName( $sName );
	}
	$rhSelf->selected();
    return $rhSelf;
}

sub setName
#*****************************************************************************
# PURPOSE:
# USAGE: 
# PASSED ARGS:
# RETURN ARGS:
# $sError - Fail Error Message
#*****************************************************************************
{
	my ( $rhSelf ) = shift; 
	my ( $sName ) = shift;
	return 0 if not $sName;

	$rhSelf->{ ORGNAME } =  $sName;
	$rhSelf->{ NAME } = uc( $sName );	
}


sub getName
#*****************************************************************************
# PURPOSE:
# USAGE: 
# PASSED ARGS:
# RETURN ARGS:
# $sError - Fail Error Message
#*****************************************************************************
{
	my ( $rhSelf ) = shift; 
	return $rhSelf->{ NAME };
}

sub getOrgName
#*****************************************************************************
# PURPOSE:
# USAGE: 
# PASSED ARGS:
# RETURN ARGS:
# $sError - Fail Error Message
#*****************************************************************************
{
	my ( $rhSelf ) = shift; 
	return $rhSelf->{ ORGNAME };
}

sub setType
#*****************************************************************************
# PURPOSE:
# USAGE: 
# PASSED ARGS:
# RETURN ARGS:
# $sError - Fail Error Message
#*****************************************************************************
{
	my ( $rhSelf ) = shift; 
	my ( $sType ) = shift;
	
	return 0 if not defined $sType;
	$rhSelf->{ TYPE } = $sType;
}

sub getType
#*****************************************************************************
# PURPOSE:
# USAGE: 
# PASSED ARGS:
# RETURN ARGS:
# $sError - Fail Error Message
#*****************************************************************************
{
	my ( $rhSelf ) = shift; 
	return $rhSelf->{ TYPE };
}

sub getNameType
#*****************************************************************************
# PURPOSE:
# USAGE: 
# PASSED ARGS:
# RETURN ARGS:
# $sError - Fail Error Message
#*****************************************************************************
{
	my ( $rhSelf ) = shift; 
	my ( $sName ) = $rhSelf->getName();	
	my ( $sType ) = $rhSelf->getType();
	
	return (  $sName, $sType );
}	

sub isSameName
#*****************************************************************************
# PURPOSE:
# USAGE: 
# PASSED ARGS:
# RETURN ARGS:
# $sError - Fail Error Message
#*****************************************************************************
{
	my ( $rhSelf ) = shift; 
	my ( $sName ) = shift;
	return 0 if not $sName;

	my $sMetaName = $rhSelf->{ NAME };
	return 0 if not $sMetaName;
	
	$sName = uc( $sName );
	return $sMetaName eq $sName ? 1 : 0; 
}	

sub isSameType
#*****************************************************************************
# PURPOSE:
# USAGE: 
# PASSED ARGS:
# RETURN ARGS:
# $sError - Fail Error Message
#*****************************************************************************
{
	my ( $rhSelf ) = shift; 
	my ( $sType ) = shift;
	return 0 if not defined $sType;

	my $sMetaType = $rhSelf->{ TYPE };
	return 0 if not defined $sMetaType;
	
	return $sMetaType eq $sType ? 1 : 0; 	
}

sub isSameNameValue
#*****************************************************************************
# PURPOSE:
# USAGE: 
# PASSED ARGS:
# RETURN ARGS:
# $sError - Fail Error Message
#*****************************************************************************
{
	my ( $rhSelf ) = shift; 
	my ( $sName, $sType ) = @_;
	
	my $sResult = $rhSelf->isSameName( $sName ); 
	if( defined $sType ){
		$sResult &&= $rhSelf->isSameType( $sType );
	}
	
	return $sResult;
}

sub equals
#*****************************************************************************
# PURPOSE:
# USAGE: 
# PASSED ARGS:
# RETURN ARGS:
# $sError - Fail Error Message
#*****************************************************************************
{
	my ( $rhSelf ) = shift; 
	my ( $rhMeta, $sUseType ) = @_;

	return 0 if not $rhMeta or 
				not ref( $rhMeta );
				
	my @aNameType = $rhMeta->getName();
	if( $sUseType ){
		my $sType = $rhMeta->getType();
		if( defined $sType ){
			push @aNameType, $sType;
		}
	}
	return $rhSelf->isSameNameValue( @aNameType );					
}	

sub selected
#*****************************************************************************
# PURPOSE:
# USAGE: 
# PASSED ARGS:
# RETURN ARGS:
# $sError - Fail Error Message
#*****************************************************************************
{
	my( $rhSelf ) = shift;      
	$rhSelf->{ SELECT } = 1;
}

sub unselected
#*****************************************************************************
# PURPOSE:
# USAGE: 
# PASSED ARGS:
# RETURN ARGS:
# $sError - Fail Error Message
#*****************************************************************************
{
	my( $rhSelf ) = shift;      
	$rhSelf->{ SELECT } = 0;
}

sub isSelected
#*****************************************************************************
# PURPOSE:
# USAGE: 
# PASSED ARGS:
# RETURN ARGS:
# $sError - Fail Error Message
#*****************************************************************************
{
	my( $rhSelf ) = shift;    	
	$rhSelf->{ SELECT }  ? 1 : 0;
}
 
sub clone
#*****************************************************************************
# PURPOSE:
# USAGE: 
# PASSED ARGS:
# RETURN ARGS:
# $sError - Fail Error Message
#*****************************************************************************
{
	my( $rhSelf ) = shift;
	
	require Storable; 
    return  Storable::dclone( $rhSelf ); 
} 

sub toString
#*****************************************************************************
# PURPOSE:
# USAGE: 
# PASSED ARGS:
# RETURN ARGS:
# $sError - Fail Error Message
#*****************************************************************************
{
	my( $rhSelf ) = shift;
	my $sName = $rhSelf->getName() || "" ;
	my $sValue = "Name: [" . $sName . "]\n";
	
	my $sType = $rhSelf->getType() || "";
	$sValue .= "Type: [" . $sType . "]\n";
	
	$sValue .= "IsSeleted: [" . $rhSelf->isSelected() . "]\n";
	return $sValue;
}	

1
__END__;
